package com.unidue.foguing.helper;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.uima.UimaContext;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.fit.component.JCasCollectionReader_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Progress;
import org.apache.uima.util.ProgressImpl;

/**
 * this class make some variable initializations and constitute a basis 
 * for reading a text file
 * @author foguing
 *
 */
public abstract class ReaderHelper extends JCasCollectionReader_ImplBase {
	
	public static final String INPUT_FILE = "InputFile";
	@ConfigurationParameter(name = INPUT_FILE , mandatory = true)
	private File inputFile;
	
	protected List<String> lines;
	protected int currentLine;
	
	/**
	 * initialization of the reader
	 */
	@Override
	public void initialize(UimaContext context)
			throws ResourceInitializationException {
		super.initialize(context);
		try {
			lines = FileUtils.readLines(inputFile);
			currentLine = 0;
		} catch (IOException e) {
			throw new ResourceInitializationException(e);
		}
		
	}

	/**
	 * this method return true if there is a next document 
	 * false otherwise
	 */
	public boolean hasNext() throws IOException, CollectionException {
		return currentLine < lines.size();
	}
	
	/**
	 * this method makes the pipeline know about the current progress
	 */
	public Progress[] getProgress() {
		return new Progress[] {new ProgressImpl(currentLine, lines.size(), "lines")};
	}

}
